package com.example.xgakj.newterminal.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.example.xgakj.mylibrary.mvp.utils.SharedPreferencesUtil;
import com.example.xgakj.newterminal.R;
import com.example.xgakj.newterminal.base.BaseActivity;
import com.example.xgakj.newterminal.base.Parameters;
import com.example.xgakj.newterminal.beans.ExitEvent;
import com.example.xgakj.newterminal.models.LoginModel;
import com.example.xgakj.newterminal.pushmodule.OnlineService;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AlarmSetActivity extends BaseActivity {


    @BindView(R.id.alarm_notice)
    ToggleButton alarmNotice;
    @BindView(R.id.tb_vibrate)
    ToggleButton tbVibrate;
    @BindView(R.id.tb_music)
    ToggleButton tbMusic;
    @BindView(R.id.tv_sound)
    TextView layoutSound;
    @BindView(R.id.topbar)
    QMUITopBar topbar;
    @BindView(R.id.tv_loginOut)
    TextView tvLoginOut;
    @BindView(R.id.tv_exit)
    TextView tvExit;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_set);
        ButterKnife.bind(this);
        iniTopbar();
        initViews();
        initListeners();


    }



    private void iniTopbar() {
        topbar = (QMUITopBar) findViewById(R.id.topbar);
        topbar.setTitle("设置");
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initListeners() {

        alarmNotice.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferencesUtil.setBoolean(mContex, Parameters.ALARM_NOTICE, isChecked);
                Intent intent=new Intent(AlarmSetActivity.this, OnlineService.class);
                if (isChecked){
                    startService(intent);

                }else {
                   stopService(intent);
                }

            }
        });
        tbMusic.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferencesUtil.setBoolean(mContex, Parameters.ALARM_MUSIC, isChecked);

            }
        });
        tbVibrate.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferencesUtil.setBoolean(mContex, Parameters.ALARM_VIBRATE, isChecked);

            }
        });


    }

    private void initViews() {
        alarmNotice.setChecked(SharedPreferencesUtil.getBoolean(mContex, Parameters.ALARM_NOTICE, true));
        tbVibrate.setChecked(SharedPreferencesUtil.getBoolean(mContex, Parameters.ALARM_VIBRATE, true));
        tbMusic.setChecked(SharedPreferencesUtil.getBoolean(mContex, Parameters.ALARM_MUSIC, true));

    }

    @OnClick({R.id.tv_loginOut, R.id.tv_exit,R.id.tv_sound})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_loginOut:
                LoginModel loginModel=new LoginModel();
                loginModel.loginOut(AlarmSetActivity.this);
                final Intent intent=new Intent(AlarmSetActivity.this, LoginActivity.class);
                startActivity(intent);
                AlarmSetActivity.this.finish();
                break;
            case R.id.tv_exit:
                new QMUIDialog.MessageDialogBuilder(mContex).setTitle("确认完全退出程序吗？")
                        .setMessage("退出后将无法接收报警推送信息！")
                        .addAction("取消", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                dialog.dismiss();
                            }
                        })
                        .addAction(0, "确认", QMUIDialogAction.ACTION_PROP_NEGATIVE, new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                Intent intent1=new Intent(mContex,OnlineService.class);
                                stopService(intent1);
                                EventBus.getDefault().post(new ExitEvent());
                                dialog.dismiss();
                                finish();
                            }
                        }).show();

                break;
            case R.id.tv_sound:
                Intent intent2 = new Intent(AlarmSetActivity.this, SettingBellRingActivity.class);
                startActivity(intent2);
                break;
            default:
        }
    }
}
